package com.example.pkmedical.Fragments;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextClock;
import android.widget.TextView;


import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.DistinguishUtil;
import com.example.pkmedical.Utils.SPUtils;
import com.example.pkmedical.Utils.SerialProtUtil;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ComfortableActivity extends BaseActivity {


    @BindView(R.id.main_house)
    ImageView mainHouse;
    @BindView(R.id.main_house_text)
    TextView mainHouseText;
    @BindView(R.id.main_date)
    TextClock mainDate;
    @BindView(R.id.head_one)
    View headOne;
    @BindView(R.id.head_two)
    View headTwo;
    @BindView(R.id.head_three)
    View headThree;
    @BindView(R.id.head_four)
    View headFour;
    @BindView(R.id.head_five)
    View headFive;
    @BindView(R.id.hip_one)
    View hipOne;
    @BindView(R.id.hip_two)
    View hipTwo;
    @BindView(R.id.hip_three)
    View hipThree;
    @BindView(R.id.hip_four)
    View hipFour;
    @BindView(R.id.hip_five)
    View hipFive;
    @BindView(R.id.foot_one)
    View footOne;
    @BindView(R.id.foot_two)
    View footTwo;
    @BindView(R.id.foot_three)
    View footThree;
    @BindView(R.id.foot_four)
    View footFour;
    @BindView(R.id.foot_five)
    View footFive;
    @BindView(R.id.head_up)
    RelativeLayout headUp;
    @BindView(R.id.head_down)
    RelativeLayout headDown;
    @BindView(R.id.hip_up)
    RelativeLayout hipUp;
    @BindView(R.id.hip_down)
    RelativeLayout hipDown;
    @BindView(R.id.foot_down)
    RelativeLayout footDown;
    @BindView(R.id.foot_up)
    RelativeLayout footUp;
    @BindView(R.id.comfortable_goback)
    Button comfortableGoback;
    byte[] mBytes;
    byte[] mSendBytes = CommandUtil.SHUSHI_DEFAULT;
    private static final String TAG = "yangshuo";
    int head, foot, hip;

    //    int head = SPUtils.getInstance().getInt("comfortable_HeadNum", 3);
//    int hip = SPUtils.getInstance().getInt("comfortable_HipNum", 3);
//    int foot = SPUtils.getInstance().getInt("comfortable_FootNum", 3);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comfortable_activity);
        ButterKnife.bind(this);
    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        if (event.getCode() == MessageWrap.SHUSHIMOSHI) {
            Log.d(TAG, "onDataReceivedShuShiActivity [ byte[] ]: " + Arrays.toString(event.getBytes()));
            mBytes = event.getBytes();
            distinguish(mBytes);
        } else {
            Log.d(TAG, "onDataReceived [ byte[] ]: NoThisData");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        head = SPUtils.getInstance().getInt("comfortable_HeadNum", 3);
        hip = SPUtils.getInstance().getInt("comfortable_HipNum", 3);
        foot = SPUtils.getInstance().getInt("comfortable_FootNum", 3);
    }

    @Override
    public void initData() {
    }

    /**
     * 返回数据识别
     */
    private void distinguish(byte[] bytes) {
        clickMethom();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                allClose();
            }
        });
        if (DistinguishUtil.distinguishUtil(bytes)) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    switch (bytes[4]) {
                        case 1:
                            headFive.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                        case 2:
                            headFour.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                        case 3:
                            headThree.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                        case 4:
                            headTwo.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                        case 5:
                            headOne.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                    }

                    switch (bytes[5]) {
                        case 1:
                            hipFive.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                        case 2:
                            hipFour.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                        case 3:
                            hipThree.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                        case 4:
                            hipTwo.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                        case 5:
                            hipOne.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                    }
                    switch (bytes[6]) {
                        case 1:
                            footFive.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                        case 2:
                            footFour.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                        case 3:
                            footThree.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                        case 4:
                            footTwo.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                        case 5:
                            footOne.setBackgroundResource(R.drawable.customized_item_back_green);
                            break;
                    }
                }
            });

        }
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void initView() {
        mainHouse.setImageResource(R.drawable.img_title_qdms);
        mainHouseText.setText("舒适模式");
        SerialProtUtil.getInstance().sendBytes(2, CommandUtil.SHUSHI_DEFAULT);

    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick({R.id.head_up, R.id.head_down, R.id.hip_up, R.id.hip_down, R.id.foot_down, R.id.foot_up, R.id.comfortable_goback})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_up:
                headDown.setClickable(true);
                if (head < 5) {
                    head += 1;
                    mSendBytes[6] = (byte) head;
                    SerialProtUtil.getInstance().sendBytes(2, mSendBytes);
                    SPUtils.getInstance().put("comfortable_HeadNum", head);
                }
                break;
            case R.id.head_down:
                headUp.setClickable(true);
                if (head > 1) {
                    head -= 1;
                    mSendBytes[6] = (byte) head;
                    SerialProtUtil.getInstance().sendBytes(2, mSendBytes);
                    SPUtils.getInstance().put("comfortable_HeadNum", head);
                }
                break;
            case R.id.hip_up:
                hipDown.setClickable(true);
                    if (hip < 5) {
                    hip += 1;
                    mSendBytes[7] = (byte) hip;
                    SerialProtUtil.getInstance().sendBytes(2, mSendBytes);
                    SPUtils.getInstance().put("comfortable_HipNum", hip);
                }
                break;
            case R.id.hip_down:
                hipUp.setClickable(true);
                if (hip > 1) {
                    hip -= 1;
                    mSendBytes[7] = (byte) hip;
                    SerialProtUtil.getInstance().sendBytes(2, mSendBytes);
                    SPUtils.getInstance().put("comfortable_HipNum", hip);
                }
                break;
            case R.id.foot_up:
                footDown.setClickable(true);
                if (foot < 5) {
                    foot += 1;
                    mSendBytes[8] = (byte) foot;
                    SerialProtUtil.getInstance().sendBytes(2, mSendBytes);
                    SPUtils.getInstance().put("comfortable_FootNum", foot);
                }
                break;
            case R.id.foot_down:
                footUp.setClickable(true);
                if (foot > 1) {
                    foot -= 1;
                    mSendBytes[8] = (byte) foot;
                    SerialProtUtil.getInstance().sendBytes(2, mSendBytes);
                    SPUtils.getInstance().put("comfortable_FootNum", foot);

                }
                break;

            case R.id.comfortable_goback:
                ComfortableActivity.this.finish();
                break;
        }
    }

    private void allClose() {
        headOne.setBackgroundResource(R.drawable.comfortable_list_back_white);
        headTwo.setBackgroundResource(R.drawable.comfortable_list_back_white);
        headThree.setBackgroundResource(R.drawable.comfortable_list_back_white);
        headFour.setBackgroundResource(R.drawable.comfortable_list_back_white);
        headFive.setBackgroundResource(R.drawable.comfortable_list_back_white);
        hipOne.setBackgroundResource(R.drawable.comfortable_list_back_white);
        hipTwo.setBackgroundResource(R.drawable.comfortable_list_back_white);
        hipThree.setBackgroundResource(R.drawable.comfortable_list_back_white);
        hipFour.setBackgroundResource(R.drawable.comfortable_list_back_white);
        hipFive.setBackgroundResource(R.drawable.comfortable_list_back_white);
        footOne.setBackgroundResource(R.drawable.comfortable_list_back_white);
        footTwo.setBackgroundResource(R.drawable.comfortable_list_back_white);
        footThree.setBackgroundResource(R.drawable.comfortable_list_back_white);
        footFour.setBackgroundResource(R.drawable.comfortable_list_back_white);
        footFive.setBackgroundResource(R.drawable.comfortable_list_back_white);
    }

    private void clickMethom() {
        if (foot <= 1) {
            footDown.setClickable(false);
        }
        if (hip <= 1) {
            hipDown.setClickable(false);
        }
        if (head <= 1) {
            headDown.setClickable(false);
        }
        if (foot >= 5) {
            footUp.setClickable(false);
        }
        if (hip >= 5) {
            hipUp.setClickable(false);
        }
        if (head >= 5) {
            headUp.setClickable(false);
        }
    }
}
